import { Inject } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { OrgInfoEntity } from '../../entity/info';
import { OrgInfoService } from '../../service/info';

/**
 * 机构信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: OrgInfoEntity,
  service: OrgInfoService,
  pageQueryOp: {
    keyWordLikeFields: ['a.name', 'a.shortName'],
    fieldEq: ['a.type', 'a.status'],
    where: async ctx => {
      const { parentId } = ctx.request.body;
      if (parentId) {
        return [['a.parentId = :parentId', { parentId }]];
      }
    },
    join: [
      {
        entity: OrgInfoEntity,
        alias: 'b',
        condition: 'a.parentId = b.id',
        type: 'leftJoin',
      },
    ],
    select: ['a.*', 'b.name as parentName'],
  },
})
export class AdminOrgInfoController extends BaseController {
  @Inject()
  orgInfoService: OrgInfoService;
}
